﻿using Sdk;
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace DiverseStudents.UI
{
    internal static partial class UICreate
    {
        public static UIButton Button(UnityAction action, string text = "按钮", string flag = "Base", bool _show = true, bool type = true)
        {
            if (settingView != null)
            {
                string path = type ? "group_bg/group_btn/btn_confirm " : "group_bg/group_btn/btn_cancel";
                var bt1 = settingView.transform.Find(path).gameObject;
                if (bt1 != null)
                {
                    var ins = GameObject.Instantiate(bt1);
                    if (ins != null)
                    {
                        var btn1 = new UIButton(ins);
                        btn1.btn.name = $"Mod_Button{flag}";
                        btn1.gameObject?.SetActive(_show);
                        btn1.AddClick(action);
                        var txt = btn1.transform.GetComponentInChildren<Text>();
                        if (txt != null)
                        {
                            txt.fontSize = 24;
                            txt.gameObject.name = $"Mod_ButtonText{flag}";
                            txt.text = text;
                        }
                        return btn1;
                    }
                }
                else
                {
                    Debug.Log("not find btn_ok");
                }
            }
            else
            {
                Debug.Log("Not find prefabs");
            }
            return CreateErrorButton(action, _show);
        }
        public static UIButton CloseButton(UnityAction action, bool _show = true)
        {
            GameObject gameObject = new GameObject("btn_close", typeof(RectTransform), typeof(Image), typeof(Button));
            var closeBtn = new UIButton(gameObject);
            gameObject.name = "Mod_Close";
            SpriteState spriteState = closeBtn.btn.spriteState;
            try
            {
                AtlasMgr.GetSprite("common4/btn_close3", delegate (Sprite _s)
                {
                    closeBtn.btn.image.sprite = spriteState.selectedSprite = _s;
                    closeBtn.btn.image.SetNativeSize();
                    closeBtn.btn.spriteState = spriteState;
                });
                AtlasMgr.GetSprite("common4/btn_close", delegate (Sprite _s)
                {
                    spriteState.highlightedSprite = _s;
                    closeBtn.btn.spriteState = spriteState;
                });
                closeBtn.AddMouseEnter(delegate
                {
                    if (closeBtn.btn.spriteState.highlightedSprite != null)
                    {
                        closeBtn.btn.image.sprite = closeBtn.btn.spriteState.highlightedSprite;
                    }
                });
                closeBtn.AddMouseExit(delegate
                {
                    if (closeBtn.btn.spriteState.selectedSprite != null)
                    {
                        closeBtn.btn.image.sprite = closeBtn.btn.spriteState.selectedSprite;
                    }
                });
            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
            //AtlasMgr.GetSprite("common4/btn_close2", delegate (Sprite _s)
            //{
            //    spriteState.pressedSprite = _s;
            //    closeBtn.btn.spriteState = spriteState;
            //});
           
            closeBtn.AddClick(action);
            closeBtn.gameObject?.SetActive(_show);
            return closeBtn;
        }

        public static Button ItemButton(UnityAction action, float width = 80, float height = 40, bool _show = true)
        {

            var gameObject_button = new GameObject("Mod_ItemButton", typeof(Button), typeof(HorizontalLayoutGroup));
            var hl= gameObject_button.GetComponent<HorizontalLayoutGroup>();
            if (hl != null)
            {
                hl.childForceExpandWidth = false;
                hl.childForceExpandHeight = false;
                hl.childControlWidth = true;
                hl.childControlHeight = true;
                hl.childScaleWidth = true;
                hl.childAlignment = TextAnchor.MiddleCenter; 
            }
            var baseRect = gameObject_button.transform as RectTransform;
            baseRect.sizeDelta = new Vector2(width, height);
            var btn = gameObject_button.GetComponent<Button>();
            if (detailIntentView != null)
            {
                var detImg = detailIntentView.transform.Find("group_data/Scroll View/Viewport/itemgroup_list/Cell_DetailIntentItem/icon_bg").gameObject.GetComponent<Image>();
                if (detImg != null)
                {
                    // var image = UICreate.CreatEmpty<Image>("Mod_ItemButtonBg");

                    // image.sprite
                   var img = GameObject.Instantiate(detImg.sprite);
                    // image.transform.SetParent(btn.transform, false);
                      btn.image  = btn.gameObject.AddComponent<Image>();
                     btn.image.sprite = img;
                }
                else
                {
                    DLog.Warring("Not Find detImg!");
                }
            }
            // var bt = new UIButton(gameObject_button);
            btn.onClick.AddListener(action);
            btn.gameObject?.SetActive(_show);
            return btn;
        }

        private static UIButton CreateErrorButton(UnityAction action, bool _show)
        {
            var gameObject_button = new GameObject("Mod_ErrorButton", typeof(RectTransform), typeof(Button), typeof(Image));
            var bt = new UIButton(gameObject_button);
            bt.AddClick(action);
            if (bt.btn.image != null)
                bt.btn.image.color = Color.white;
            bt.gameObject?.SetActive(_show);
            return bt;

        }
    }
}
